<?php
/**
 * Created by PhpStorm.
 * User: lrs
 * Date: 2018/2/11
 * Time: 19:31
 */
namespace Admin\Controller;
use Common\Controller\AuthController;

class UploadController extends AuthController
{
    /**
     * 批量上传文件
     * @return string
     */
    public function fileUploads()
    {
        $file = $_FILES;
        if ($file) {
            $fileUpload = new \Org\Upload\FileUpload();
            for ($a = 0; $a < count($file['upload']['tmp_name']); $a++) {
                if ($file['upload']['tmp_name'][$a]) {
                    $name[] = $fileUpload->fileUpload($file['upload']['name'][$a], $file['upload']['tmp_name'][$a]);
                } else {
                    continue;
                }
            }
        } else {
            return "";
        }
        echo json_encode($name);
    }

    /**
     * 删除一个文件，暴露的controller
     * @param $fileName
     */
    public function fileDelete($fileName)
    {
        // 只允许删除 upload 目录下文件
        if(strpos($fileName,'/upload/') !== false){
            $fileName = "upload/".strtr($fileName, array("/upload/" => ""));
            unlink($fileName);
        }
        echo json_encode(array("status"=>1,"message"=>"成功","data"=>array("fileName"=>$fileName)));
    }

    /**
     * 删除一个文件
     * @param $fileName
     */
    public function fileDel($fileName)
    {
        // 只允许删除 upload 目录下文件
        if(strpos($fileName,'/upload/') !== false){
            $fileName = "upload/".strtr($fileName, array("/upload/" => ""));
            unlink($fileName);
        }
    }
}
